package com.chen.product.service.impl;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chen.model.dto.h5.ProductSkuDto;
import com.chen.model.entity.product.Product;
import com.chen.model.entity.product.ProductDetails;
import com.chen.model.entity.product.ProductSku;
import com.chen.model.vo.h5.ProductItemVo;
import com.chen.product.mapper.ProductDetailsMapper;
import com.chen.product.mapper.ProductMapper;
import com.chen.product.mapper.ProductSkuMapper;
import com.chen.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cgj
 * @date 4/14/25 7:52 PM
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    /**
     *     分页条件查询商品SKU
     * @param pageNum
     * @param pageSize
     * @param productSkuDto
     * @return
     */
    @Override
    public PageInfo<ProductSku> selectByPageAndCondition(Integer pageNum, Integer pageSize, ProductSkuDto productSkuDto) {
        PageHelper.startPage(pageNum,pageSize); //开启分页
      List<ProductSku> productSkuList= productSkuMapper.selectByPageAndCondition(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    /**
     * 根据skuID查询商品的详情
     * @param skuId
     * @return
     */
    @Override
    public ProductItemVo getItem(Long skuId) {
        // 根据skuId查询详情
      ProductSku productSku= productSkuMapper.selectById(skuId);
      //根据productId查询product详情
        Product product=productMapper.selectById(productSku.getProductId());
        //根据productId查询详情
        ProductDetails productDetails=productDetailsMapper.selectById(productSku.getProductId());
        // 根据productId 查询
     List<ProductSku> productSkuList=   productSkuMapper.selectByProductId(productSku.getProductId());
     //  拼凑map集合 key 是 sku_spec, value是Sku表的id
        HashMap<String, Object> map = new HashMap<>();
        for (ProductSku sku : productSkuList) {
             map.put(sku.getSkuSpec(),sku.getId());
        }
        ProductItemVo productItemVo = new ProductItemVo();
        //封装product
        productItemVo.setProduct(product);
        //封装productSku
        productItemVo.setProductSku(productSku);
        //商品规格对应商品skuId信息
        productItemVo.setSkuSpecValueMap(map);
        //商品轮播图列表 http://139.198.127.41:9000/spzx/20230525/f1d7433dbe88c25e.jpg.avif,http://139.198.127.41:9000/spzx/20230525/4b5a68a9bfbd0795.jpg.avif,http://139.198.127.41:9000/spzx/20230525/13beb824e9dcfc88.jpg.avif,http://139.198.127.41:9000/spzx/20230525/3976e9cfaf7c6ceb.jpg.avif,http://139.198.127.41:9000/spzx/20230525/35b3b7e2e67b98b2.jpg.avif,http://139.198.127.41:9000/spzx/20230525/c8f2eae0d36b6270.jpg.avif,http://139.198.127.41:9000/spzx/20230525/fd5c6bfa6f3c4d43.jpg.avif
        String sliderUrls = product.getSliderUrls();
        String[] sliderUrlsArray = sliderUrls.split(",");
        productItemVo.setSliderUrlList(Arrays.stream(sliderUrlsArray).toList());
        // detailsImageUrlList
        productItemVo.setDetailsImageUrlList(Arrays.stream(productDetails.getImageUrls().split(",")).toList());
        //[{"key":"内存","valueList":["8G","16G","32G"]}] SpecValue
        String specValue = product.getSpecValue();
        productItemVo.setSpecValueList(JSON.parseArray(specValue));


        return productItemVo;
    }

    /**
     * 根据skuId,查询sku
     * @param skuId
     * @return
     */
    @Override
    public ProductSku getProductSkuBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.selectById(skuId);
        return productSku;
    }

}
